package com.basin.micro.auth.module.menu.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.basin.common.core.consts.StatusConst;
import com.basin.common.core.exception.BasinException;
import com.basin.common.core.response.ResponseResult;
import com.basin.common.mp.base.BaseController;
import com.basin.micro.auth.module.menu.mapper.MenuMapper;
import com.basin.micro.auth.module.menu.pojo.dto.MenuDTO;
import com.basin.micro.auth.module.menu.pojo.entity.MenuEntity;
import com.basin.micro.auth.module.menu.pojo.enums.MenuTypeEnum;
import com.basin.micro.auth.module.menu.pojo.vo.MenuTreeVO;
import com.basin.micro.auth.module.menu.repository.MenuRepository;
import com.basin.micro.auth.module.menu.service.MenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: Sam ZHONG | sammy1997@aliyun.com
 */
@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
@Slf4j
public class MenuController extends BaseController<MenuRepository, MenuEntity, MenuMapper> {
    private final MenuService menuService;

    @GetMapping("/getAllMenu")
    public ResponseResult<List<MenuTreeVO>> getMenuTree(@RequestParam("sys_code") String sysCode) {
        List<MenuTreeVO> menuTree = menuService.getMenuTree(sysCode);
        return ResponseResult.success(menuTree);
    }

    @GetMapping("/getCurrentUserMenus")
    public ResponseResult<List<MenuTreeVO>> getCurrentUserMenus() {
        List<MenuTreeVO> menuTree = new ArrayList<>() {{
            MenuDTO node1 = new MenuDTO();
            node1.setMenuRoute("/menu-manage");
            node1.setMenuName("菜单管理");
            MenuDTO node2 = new MenuDTO();
            node2.setMenuRoute("/employee-manage");
            node2.setMenuName("员工管理");
            MenuDTO node3 = new MenuDTO();
            node3.setMenuRoute("/config-manage");
            node3.setMenuName("配置管理");
            add(new MenuTreeVO(node1));
            add(new MenuTreeVO(node2));
            add(new MenuTreeVO(node3));
        }};
        return ResponseResult.success(menuTree);
    }

    @Override
    public void beforeAdd(MenuEntity entity) {
        this.commonValid(entity);
    }

    @Override
    public void beforeUpdate(MenuEntity entity) {
        commonValid(entity);
    }

    private void commonValid(MenuEntity entity) {
        Integer type = entity.getMenuType().getCode();
        // 非目录级别需要route参数
        if (!Objects.equals(type, MenuTypeEnum.MENU_TYPE_0.getCode()) && ObjectUtil.isEmpty(entity.getMenuRoute())) {
            throw new BasinException("非目录类型需要填写route");
        }
        // 父类真实
        String parentCode = entity.getParentCode();
        if (!ObjectUtil.isEmpty(parentCode)) {
            MenuEntity one = this.getRepository().getOne(Wrappers.<MenuEntity>lambdaQuery()
                    .eq(MenuEntity::getMenuCode, parentCode)
                    .eq(MenuEntity::getMenuStatus, StatusConst.ENABLE));
            if (one == null) throw new BasinException("上级菜单不存在或未启用");
            // 除了目录可以嵌套 其他等级不能一样
            if ((!Objects.equals(MenuTypeEnum.MENU_TYPE_0.getCode(), one.getMenuType().getCode())
                    && Objects.equals(type, one.getMenuType().getCode()))
                    || Objects.equals(MenuTypeEnum.MENU_TYPE_2.getCode(), one.getMenuType().getCode())) {
                throw new BasinException("不支持嵌套菜单类型");
            }
        }
    }
}
